package com.qf.controller;

import com.qf.entity.Order;
import com.qf.entity.User;
import com.qf.service.OrderService;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/16 22:08
 * @Version 1.0
 */
@WebServlet(name = "OrderController",value = "/order")
public class OrderController extends BaseController {
    /**
     * 一个订单一定对应一个包裹
     * ：在c2c中：他是根据商家来划分的
     * b2c项目：看需求：比如京东自营/京东超市/天猫超市/:仓库位置进行划分的
     * 小米商城：不考虑仓库，不考虑其他商家问价，在小米商城同一个下的订单，默认发一个包裹，一个订单
     */
    private OrderService service=new OrderServiceImpl();

    //生成订单
    public String addOrder(HttpServletRequest request,HttpServletResponse response){
        //拿参数
        String strAid = request.getParameter("aid");//地址id
        String strSum=request.getParameter("sum");//商品总计
        User user = (User) request.getSession().getAttribute("loginUser");
        Order order=new Order();
        order.setOrderSum(new BigDecimal(strSum));
        order.setAid(Integer.parseInt(strAid));
        order.setUid(user.getId());

        //调用service
        int result=service.addOrder(order);
        if(result==1){
            return "redirect:orderSuccess.jsp";
        }else{
            request.setAttribute("msg","生成订单失败");
            return "forward:error/error.jsp";
        }

    }


    public String getOrderList(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        List<Order> list= service.getOrderList(user.getId());
        request.setAttribute("orderList",list);
        return "forward:orderList.jsp";
    }

    /**
     * 获取订单的详情
     * @param request
     * @param response
     * @return
     */
    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){
        String strOid = request.getParameter("oid");
        Order order= service.getOrder(Integer.parseInt(strOid));
        request.setAttribute("order",order);
        return "forward:orderDetail.jsp";
    }

}
